#!/bin/bash
# This script sets up the Apache server to access
# websm help

#check if the apache server is installed

package=`rpm -q -a | grep apache | awk '{print $1}'`
#echo "The package is : $package"
if [ "$package" != "" ]
then
        #echo "Apache Package is installed....."
        #check if the httpd.conf file exists
        if [ -f /etc/httpd/conf/httpd.conf ]
        then
                #echo "the config file exists"
		#check if the ServerName is already set
		hname=""
	        hname=`grep -v ^# /etc/httpd/conf/httpd.conf | grep ServerName | cut -d ' ' -f 2`	
		if [ "$hname" != "" ]
		then 

		     #echo "Server Name exists hence needs to be changed"
		     awk '// {if ($1 ~ /^ServerName/) {printf("%s\n", hn)} else {print}}' hn="ServerName $HOSTNAME" /etc/httpd/conf/httpd.conf > /etc/httpd/conf/httpd.mod
		     cp /etc/httpd/conf/httpd.mod /etc/httpd/conf/httpd.conf
		     rm /etc/httpd/conf/httpd.mod
		
		else	

                     # add the Server Name to the end of the file 
                     #echo "The host name is $HOSTNAME"
                     echo "ServerName $HOSTNAME" >> /etc/httpd/conf/httpd.conf
		fi
	    fi

		if [ ! -L /var/www/html/help ]
		then
		      #echo "Setting up the link"
		      ln -s /opt/hsc/help /var/www/html
		fi
		#check if the apache server is running
		run=`ps -ef | grep httpd`
		if [ "$run" != "" ]
		then
			#echo "the httpd server is running hence need to stop"

                        /etc/rc.d/init.d/httpd stop > /dev/null 2>&1
		fi
		#start the apache server
		/etc/rc.d/init.d/httpd start > /dev/null 2>&1

		#Add the command to start the Apache server in inittab
		entry=`grep 'apac:2345:once:/etc/rc.d/init.d/httpd start' /etc/inittab`		
		if [ "$entry" == "" ]
 		then
		      #echo "Add the lines to inittab" 
		      echo "apac:2345:once:/etc/rc.d/init.d/httpd start" >> /etc/inittab
		fi
	#echo "done apache setup"

fi
